package com.yunchang.open.iploaction.utils;

import com.yunchang.open.iploaction.Location;

/**
 * 地区工具类。
 * Created by jasontujun on 2019/6/24.
 */
public abstract class LocationUtil {

    /**
     * 两个地区，是否一致
     *
     * @param location1    地区1
     * @param location2    地区2
     * @param defaultValue 如果地区为空或不存在，返回的默认值
     * @return 地区一致返回true；否则返回false
     */
    public static boolean isSameLocation(Location location1, Location location2, boolean defaultValue) {
        if (location1 == null || location2 == null) {
            return defaultValue;
        }
        // 先比较市(都不为空时，直接比较返回结果)
        if (!isUnknownPlace(location1.getCity()) && !isUnknownPlace(location2.getCity())) {
            return location1.getCity().equals(location2.getCity());
        }
        // 再比较省(都不为空时，直接比较返回结果)
        if (!isUnknownPlace(location1.getProvince()) && !isUnknownPlace(location2.getProvince())) {
            return location1.getProvince().equals(location2.getProvince());
        }
        // 再比较国家(都不为空时，直接比较返回结果)
        if (!isUnknownPlace(location1.getCountry()) && !isUnknownPlace(location2.getCountry())) {
            return location1.getCountry().equals(location2.getCountry());
        }
        // 如果地区都为空，则返回默认值
        return defaultValue;
    }

    /**
     * 两个地区，是否一致
     *
     * @param location1    地区1
     * @param location2    地区2
     * @param defaultValue 如果地区为空或不存在，返回的默认值
     * @return 地区一致返回true；否则返回false
     */
    public static boolean isWholeSameLocation(Location location1, Location location2, boolean defaultValue) {
        if (location1 == null || location2 == null) {
            return defaultValue;
        }
        // 先比较市(其中之一不为空时，直接比较返回结果)
        if (!isUnknownPlace(location1.getCity()) || !isUnknownPlace(location2.getCity())) {
            return location1.getCity().equals(location2.getCity());
        }
        // 再比较省(其中之一不为空时，直接比较返回结果)
        if (!isUnknownPlace(location1.getProvince()) || !isUnknownPlace(location2.getProvince())) {
            return location1.getProvince().equals(location2.getProvince());
        }
        // 再比较国家(其中之一不为空时，直接比较返回结果)
        if (!isUnknownPlace(location1.getCountry()) || !isUnknownPlace(location2.getCountry())) {
            return location1.getCountry().equals(location2.getCountry());
        }
        // 如果地区都为空，则返回默认值
        return defaultValue;
    }


    public static boolean isSameProvince(Location location1, Location location2, boolean defaultValue) {
        if (location1 == null || location2 == null) {
            return defaultValue;
        }
        // 再比较省(都不为空时，直接比较返回结果)
        if (!isUnknownPlace(location1.getProvince()) && !isUnknownPlace(location2.getProvince())) {
            return location1.getProvince().equals(location2.getProvince());
        }
        // 再比较国家(都不为空时，直接比较返回结果)
        if (!isUnknownPlace(location1.getCountry()) && !isUnknownPlace(location2.getCountry())) {
            return location1.getCountry().equals(location2.getCountry());
        }
        // 如果地区都为空，则返回默认值
        return defaultValue;
    }

    /**
     * 三个地区，是否一致.
     *
     * @param location1    地区1
     * @param location2    地区2
     * @param location3    地区3
     * @param defaultValue 如果地区为空或不存在，返回的默认值
     * @return 地区一致返回true；否则返回false
     */
    public static boolean isSameLocation(Location location1, Location location2, Location location3, boolean defaultValue) {
        return isSameLocation(location1, location2, defaultValue) &&
                isSameLocation(location2, location3, defaultValue) &&
                isSameLocation(location1, location3, defaultValue);
    }

    public static boolean isUnknownLocation(Location location) {
        return location == null || (Location.UNKNOWN.equalsIgnoreCase(location.getCountry()) &&
                Location.UNKNOWN.equalsIgnoreCase(location.getProvince()) && Location.UNKNOWN.equalsIgnoreCase(location.getCity()));
    }

    public static boolean isUnknownPlace(String place) {
        return place == null || place.length() == 0 || Location.UNKNOWN.equalsIgnoreCase(place);
    }
}
